// pages/index/index.js
Page({
  data: {
    latitude: 39.911057,
    longitude: 116.484143,
    scale: 10,
    center: {
      latitude: 39.911057,
      longitude: 116.484143,
    },
    isRefresh: false,
    markers: []
  },
  onLoad() {
    this.getMarkerList()
  },
  goSearch() {
    wx.navigateTo({
      url: '../search/search',
    })
  },
  scroll(e) {
    // console.log(e);
  },
  // onPullDownRefresh() {
  //   this.onShow()
  // },
  refresh() {
    this.onLoad()
    this.setData({
      isRefresh: true
    })
  },
  mapChange(e) {
    console.log(e);
  },
  bindregionchange(e) {
    console.log(1);
    if (e.detail.centerLocation) {
      this.setData({
        center: {
          latitude: e.detail.centerLocation.latitude,
          longitude: e.detail.centerLocation.longitude
        },
      })
    }
  },
  bindmarkertap(e) {
    console.log(e);
    this.data.markers.forEach(item => {
      if (e.markerId === item.id) {
        console.log(item);
        wx.openLocation({
          latitude: item.latitude,
          longitude: item.longitude,
          scale: 28
        })
      }
    })
  },
  position() {
    let _this = this
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        _this.setData({
          latitude: latitude,
          longitude: longitude,
        })
      }
    })
  },
  addmarker() {
    let position = this.data.center
    wx.navigateTo({
      url: "../addmarker/addmarker?position=" + JSON.stringify(position),
    })
  },
  getUserProfile() {
    wx.getUserProfile({
      desc: '用于完善会员资料',
      complete(e) {
        if (e.errMsg.search("ok")) {
          console.log(e);
          try {
            wx.setStorageSync('login', JSON.stringify(e.userInfo))
            wx.showToast({
              title: '储存成功',
              icon: 'success'
            })
          } catch (e) {
            wx.showToast({
              title: '储存失败',
              icon: 'error'
            })
          }
        }
      }
    })
  },
  getMarkerList() {
    let that = this;
    wx.cloud.callFunction({
      name: 'getMarkerList',
      success: function (res) {
        wx.stopPullDownRefresh()
        wx.hideLoading()
        wx.hideNavigationBarLoading();
        that.setData({
          isRefresh: false
        })
        let newMarkers = []
        res.result.data.forEach(e => {
          e.width = 50
          e.height = 50
          newMarkers.push(e)
        })
        newMarkers.sort(function (a, b) {
          return a.ctime < b.ctime ? 1 : -1
        });
        that.setData({
          markers: newMarkers
        })
        wx.showToast({
          title: '数据更新成功',
          icon: "success"
        })
      },
      fail(err) {
        wx.showToast({
          title: '数据拉取失败',
          icon: "error"
        })
      }
    })
  },
  add() {
    let num = this.data.scale + 1
    this.setData({
      scale: num
    })
  },
  minus() {
    let num = this.data.scale - 1
    this.setData({
      scale: num
    })
  },
  goNavigation(e) {
    console.log(e.target.dataset.id);
    this.data.markers.forEach(item => {
      if (e.target.dataset.id === item.id) {
        console.log(item);
        wx.openLocation({
          latitude: item.latitude,
          longitude: item.longitude,
          scale: 20
        })
      }
    })
  }
})